package com.stox.ui.javafx.charting.event.handler;

import javafx.event.EventHandler;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.stox.event.AbstractModelSelectionEvent;
import com.stox.ui.javafx.charting.view.ChartingView;

@Component
public class ChartingViewKeyEventHandler implements EventHandler<KeyEvent> {

	@Autowired
	private ApplicationContext applicationContext;
	
	@Override
	public void handle(KeyEvent event) {
		System.out.println(event);
		if(KeyCode.PAGE_DOWN == event.getCode() || KeyCode.PAGE_UP == event.getCode()) {
			ChartingView chartingView = (ChartingView)event.getSource();
			if(null != chartingView.getAbstractModelNavigable()){
				chartingView.getAbstractModelNavigable().next(KeyCode.PAGE_DOWN == event.getCode());
				applicationContext.publishEvent(new AbstractModelSelectionEvent(chartingView.getAbstractModelNavigable()));
			}
		}

	}
}
